package com.wang.mybatis3;

import com.wang.dao.UserDao;
import com.wang.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * SqlSessionFactoryBuilder：建议方法级别，创建完SqlSessionFactory即可销毁
 * SqlSessionFactory：application级别，由SqlSessionFactoryBuilder创建，建议一个应用中只创建一次，如果需要连多个数据库，则每个数据源对应一个SqlSessionFactory
 * SqlSession：方法级别
 * Mapper：方法级别
 * @author wly
 * @date 2018/8/30 19:31
 */
public class Test {
    public static void main(String[] args) throws IOException {
        String resource = "mybatisConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sessionFactory.getConfiguration().getEnvironment().getDataSource();
        SqlSession sqlSession = sessionFactory.openSession();
        try {
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = userDao.selectUser(1);
            System.out.println(user);
        } finally {
            sqlSession.close();
        }
    }
}
